﻿using System.Collections;
using System.Threading;

namespace Customer.Core.Data.DataContextStorage
{
    public class ThreadDataContextStorageContainer : IDataContextStorageContainer
    {
        private static readonly Hashtable dataContexts = new Hashtable();

        public CustomerEntities GetDataContext()
        {
            CustomerEntities libraryDataContext = null;
            if (dataContexts.Contains(GetThreadName()))
                libraryDataContext =
                (CustomerEntities)dataContexts[GetThreadName()];
            return libraryDataContext;
        }

        public void Store(CustomerEntities libraryDataContext)
        {
            if (dataContexts.Contains(GetThreadName()))
                dataContexts[GetThreadName()] = libraryDataContext;
            else
                dataContexts.Add(GetThreadName(), libraryDataContext);
        }

        private static string GetThreadName()
        {
            return (Thread.CurrentThread == null || string.IsNullOrEmpty(Thread.CurrentThread.Name)) ? "AuthenThreadName" : Thread.CurrentThread.Name;
        }
    }
}
